<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRole.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRole.state" />
            <span v-else> {{ row.state===1?'启用':'禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRole.description" size="mini" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="primary" @click="editRole(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text">设置权限</el-button>
              <el-button type="text" @click="row.isEdit=true">编辑</el-button>
              <el-popconfirm
                title="确认删除吗？"
                confirm-button-type="primary"
                cancel-button-type="text"
                @onConfirm="delRole(row.id)"
              >
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>

            </template>
          </template>

        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="queryData.page"
          :page-sizes="[4, 8, 10, 20]"
          :page-size="queryData.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="SizeChange"
          @current-change="CurrentChange"
        />
      </el-row>
    </div>
    <addRole ref="addRole" @updataList="initData" />
  </div>
</template>
<script>
import { getRoleListApi, delRoleApi, editRoleApi } from '@/api/role'
import addRole from './components/addRole'
export default {
  name: 'Role',
  components: {
    addRole
  },
  data() {
    return {
      list: [],
      queryData: {
        pagesize: 4,
        page: 1
      },
      total: 0

    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getRoleListApi(this.queryData)
      // console.log(res)
      // 数据加工处理
      // res.rows = res.rows.map(item => {
      //   return { ...item, isEdit: false }// 给每行都添加一个isEdit控制条件渲染
      // })
      this.list = res.rows
      this.list.forEach(item => {
        // item.isEdit = false错误写法。不具备响应式
        this.$set(item, 'isEdit', false)// 正确写法。具备响应式
        this.$set(item, 'editRole', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.total = res.total
    },
    SizeChange(val) {
      this.queryData.pagesize = val
      this.initData()
    },
    CurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    async delRole(id) {
      await delRoleApi(id)
      this.$message.success('删除成功')
      if (this.queryData.page > 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    async editRole(row) {
      if (!row.editRole.name || !row.editRole.description) return this.$message.warning('请输入名称或描述')
      await editRoleApi({ ...row.editRole, id: row.id })
      this.$message.success('操作成功')
      Object.assign(row, { ...row.editRole, isEdit: false })
    }

  }
  // isEdit(row) {
  //   row.isEdit = true
  // }

}
</script>
<style  scoped>
  .app-container{
    padding: 30px;
  }
  .role-operate{
    padding: 10px;
  }
</style>
